<?php
// +----------------------------------------------------------------------
// | saiadmin [ saiadmin快速开发框架 ]
// +----------------------------------------------------------------------
// | Author: your name
// +----------------------------------------------------------------------
namespace app\cms\controller;

use plugin\saiadmin\basic\BaseController;
use app\cms\logic\UsersLogic;
use app\cms\model\UsersAuthen;
use app\cms\validate\UsersValidate;
use support\Request;
use support\Response;

/**
 * 基础信息控制器
 */
class UsersController extends BaseController
{
    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->logic = new UsersLogic();
        $this->validate = new UsersValidate;
        parent::__construct();
    }

    /**
     * 数据列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        $where = $request->more([
            ['username', ''],
            // ['realname', ''],
            // ['email', ''],
            // ['company_name', ''],
        ]);
        $query = $this->logic->search($where);
        if (!empty($where['username'])) {
            $query->whereOr('realname', 'like', '%' . $where['username'] . '%')
                ->whereOr('phone', 'like', '%' . $where['username'] . '%')
                ->whereOr('email', 'like', '%' . $where['username'] . '%')
                ->whereOr('company_name', 'like', '%' . $where['username'] . '%');
        }
        $query->with(['authentic']);
        $data = $this->logic->getList($query);
        return $this->success($data);
    }

    /**
     * 用户数据列表
     * @param Request $request
     * @return Response
     */
    public function getUserList(): Response
    {
        $data = $this->logic->where('status', 1)->field('id,username,realname')->order('id', 'desc')->select();
        return $this->success($data);
    }


    /**
     * 会员审核-认证信息审核
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-13
     * @param Request $request
     * @return void
     */
    public function auditUserAuthStatus(Request $request)
    {
        $id = $request->input('id', '');
        $status = $request->input('status', '');
        $reason = $request->input('reason', '');
        $user = $this->logic->where('id', $id)->find();
        $userAuth = UsersAuthen::where('user_id', $user->id)->update([
            'status' => $status,
            'reason' => $reason,
            'examine_time' => date('Y-m-d H:i:s', time())
        ]);

        if ($userAuth) {
            return $this->success('操作成功');
        } else {
            return $this->fail('操作失败');
        }
    }
}
